/*
 *  Copyright 2019-2020 Zheng Jie
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package me.zhengjie.rest;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import me.zhengjie.annotation.Log;
import me.zhengjie.domain.KwStudent;
import me.zhengjie.kwService.StudentService;
import me.zhengjie.pojo.entity.Student;
import me.zhengjie.service.KwStudentService;
import me.zhengjie.service.dto.KwStudentQueryCriteria;
import me.zhengjie.util.BaseResponse;
import me.zhengjie.util.ruoyiexcel.ExcelUtil;
import me.zhengjie.util.ruoyiexcel.FileTypeUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;

/**
 * @author LongYang
 * @website https://el-admin.vip
 * @date 2021-06-28
 **/
@RestController
@RequiredArgsConstructor
@Api(tags = "考生信息表前端控制器管理")
@RequestMapping("/api/kwStudent")
public class KwStudentController {

    private final KwStudentService kwStudentService;
    private final StudentService studentService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('kwStudent:list')")
    public void download(HttpServletResponse response, KwStudentQueryCriteria criteria) throws IOException {
        kwStudentService.download(kwStudentService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询考生信息表前端控制器")
    @ApiOperation("查询考生信息表前端控制器")
    @PreAuthorize("@el.check('kwStudent:list')")
    public ResponseEntity<Object> query(KwStudentQueryCriteria criteria, Pageable pageable) {
        return new ResponseEntity<>(kwStudentService.queryAll(criteria, pageable), HttpStatus.OK);
    }

    @GetMapping("/list")
    @Log("查询考生信息表前端控制器")
    @ApiOperation("查询考生信息表前端控制器")
    public ResponseEntity<Object> queryList(KwStudentQueryCriteria criteria, Pageable pageable) {
        return new ResponseEntity<>(kwStudentService.queryAll(criteria, pageable), HttpStatus.OK);
    }

    @PostMapping
    @Log("新增考生信息表前端控制器")
    @ApiOperation("新增考生信息表前端控制器")
    @PreAuthorize("@el.check('kwStudent:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody KwStudent resources) {
        return new ResponseEntity<>(kwStudentService.create(resources), HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改考生信息表前端控制器")
    @ApiOperation("修改考生信息表前端控制器")
    @PreAuthorize("@el.check('kwStudent:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody KwStudent resources) {
        kwStudentService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("删除考生信息表前端控制器")
    @ApiOperation("删除考生信息表前端控制器")
    @PreAuthorize("@el.check('kwStudent:del')")
    @DeleteMapping
    public ResponseEntity<Object> delete(@RequestBody Integer[] ids) {
        kwStudentService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }


    /*-------------------------------------------------------------------------------*/
    @ApiOperation("考生信息导出")
    @PostMapping("/ruoyiExport")
    @PreAuthorize("@el.check('kwStudent:ruoyiExport')")
    public void export(HttpServletResponse response, @RequestBody List<KwStudent> kwStudents) throws IOException {
        List<Student> list = new LinkedList<>();
        if (kwStudents.size() != 0 && kwStudents != null) {
            for (KwStudent kwStudent : kwStudents) {
                Student vo = new Student();
                BeanUtils.copyProperties(kwStudent, vo);
                list.add(vo);
            }
        } else {
            list = studentService.selectList();
        }
        ExcelUtil<Student> util = new ExcelUtil<>(Student.class);
        util.exportExcel(response, list, "考生信息导出");
        /*util.exportExcel(list,"考生信息导出");*/

    }

    @ApiOperation("考生信息导入")
    @PostMapping("/ruoyiImport")
    @PreAuthorize("@el.check('kwStudent:ruoyiImport')")
    public BaseResponse ruoYiImport(@RequestParam(value = "file") MultipartFile file) {
        if (file.isEmpty()) {
            return BaseResponse.failed("导入了空文件");
        }
        String suffix = FileTypeUtils.getFileType(file.getResource().getFilename());
        if (!"xls".equals(suffix) && !"xlsx".equals(suffix)) {
            return BaseResponse.failed("请导入xls，xlsx文件");
        }
        ExcelUtil<Student> util = new ExcelUtil<>(Student.class);
        try {
            List<Student> students = util.importExcel(file.getInputStream());
            if (students.isEmpty()) {
                return BaseResponse.failed("导入文件为空");
            }
            studentService.saveImport(students);
            return BaseResponse.ok("导入成功");
        } catch (Exception e) {
            e.printStackTrace();
            return BaseResponse.failed("导入失败：" + e.getMessage());
        }
    }

    @ApiOperation("考生信息模板下载")
    @GetMapping("/importKwStudent")
    @PreAuthorize("@el.check('kwStudent:importKwStudent')")
    public void importKwExam(HttpServletResponse response) throws Exception{
        ExcelUtil<Student> excelUtil = new ExcelUtil<>(Student.class);
        excelUtil.importTemplateExcel(response,"考生信息");
    }

    @ApiOperation("查询所有考生信息")
    @GetMapping("/getAllStudent")
    public BaseResponse getAllStudent() {
        return BaseResponse.ok(studentService.selectList());
    }
}